package com.wan37.logic.backpack.weaponry;

import com.wan37.common.uuid.UUIDUtil;
import com.wan37.logic.backpack.weaponry.service.WeaponryService;
import com.wan37.logic.db.AsynDBManager;
import com.wan37.logic.domain.Result;
import com.wan37.logic.domain.entity.UserWeaponryDO;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.domain.vo.WeaponryVO;
import com.wan37.logic.email.EmailManager;
import com.wan37.logic.email.domain.vo.AttachmentVO;
import com.wan37.logic.email.domain.vo.EmailVO;
import com.wan37.logic.listener.EventDispatcher;
import com.wan37.logic.listener.event.EventType;
import com.wan37.logic.listener.event.user.UserGetWeaponryEvent;
import com.wan37.logic.service.UserWeaponryService;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.BackpackLabel;
import com.wan37.protobuf.constant.CommonBackpack;
import com.wan37.protobuf.message.client2server.game.GameEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : luoyong
 * @date : 2020-07-28 21:43
 **/
@Component
public class WeaponryManager {
    @Autowired
    private UserServiceCache userServiceCache;
    @Autowired
    private AsynDBManager asynDBManager;
    @Autowired
    private UserWeaponryService userWeaponryService;
    @Autowired
    private EmailManager emailManager;
    @Autowired
    private RouteUtils routeUtils;

    /**
     * 普通装备添加，装备背包满了就进行邮件发送！
     * @param userId
     * @param weaponryVO
     * @return
     */
    public Result addWeaponry(String userId, WeaponryVO weaponryVO){
        long numberMap = userServiceCache.getUserServiceMap().get(userId).getUserVO().getBackpackNumber();
        Map<String, WeaponryVO> weaponryVOMap = userServiceCache.getUserServiceMap().get(userId).getWeaponryVOMap();
        if (weaponryVOMap.size() + 1 > numberMap){
            EmailVO emailVO = new EmailVO();
            emailVO.setEmailId(UUIDUtil.getUUID());
            emailVO.setIsRead(1);
            emailVO.setReceiverId(userId);
            emailVO.setSenderId("admin");
            emailVO.setContent("交易成功，背包满了的装备发放,请清空背包后领取！");
            emailVO.setCreateTime(System.currentTimeMillis());
            AttachmentVO attachmentVO = new AttachmentVO();
            attachmentVO.setType(BackpackLabel.WEAPONRY);
            attachmentVO.setEmailId(emailVO.getEmailId());
            attachmentVO.setNumber(1);
            attachmentVO.setStrongLevel(weaponryVO.getLevel());
            attachmentVO.setBackpackId(weaponryVO.getBackpackId());
            List<AttachmentVO> attachmentVOList = new ArrayList<>();
            attachmentVOList.add(attachmentVO);
            emailVO.setAttachmentVOList(attachmentVOList);
            List<String> userIdList = new ArrayList<>();
            userIdList.add(userId);
            emailManager.sendEmail(emailVO, userIdList);
            return new Result(0, "背包已满,装备已发送至邮件中！");
        }else {
            weaponryVOMap.put(weaponryVO.getSelfWeaponryId(), weaponryVO);
            UserWeaponryDO userWeaponryDO = new UserWeaponryDO(userId, weaponryVO);
            asynDBManager.distributeWork(userId, ()->userWeaponryService.save(userWeaponryDO));
            EventDispatcher.getInstance().fireEvent(new UserGetWeaponryEvent(EventType.GET_WEAPONRY, userId));
            MessageVO messageVO = new MessageVO(userId);
            routeUtils.sendWeaponryResponse(CommonBackpack.WEAPONRY_SUCCESS, userId,
                    userId + "装备信息获取成功！", new ArrayList<>(userServiceCache
                            .getUserServiceMap().get(userId).getWeaponryVOMap().values()), messageVO);
            return new Result(1, "");
        }
    }

    /**
     * email领取物品的邮件添加
     * @param userId
     * @param newWeaponryVO
     * @return
     */
    public Result emailAddWeaponry(String userId, List<WeaponryVO> newWeaponryVO){
        long numberMap = userServiceCache.getUserServiceMap().get(userId).getUserVO().getBackpackNumber();
        Map<String, WeaponryVO> weaponryVOMap = userServiceCache.getUserServiceMap().get(userId).getWeaponryVOMap();

        if (weaponryVOMap.size() + newWeaponryVO.size() > numberMap){
            return new Result(0, "背包已满！");
        }else {
            for (WeaponryVO weaponryVO : newWeaponryVO){
                weaponryVOMap.put(weaponryVO.getSelfWeaponryId(), weaponryVO);
                UserWeaponryDO userWeaponryDO = new UserWeaponryDO(userId, weaponryVO);
                asynDBManager.distributeWork(userId, ()->userWeaponryService.save(userWeaponryDO));
                EventDispatcher.getInstance().fireEvent(new UserGetWeaponryEvent(EventType.GET_WEAPONRY, userId));
            }
            return new Result(1, "");
        }
    }

    public boolean reduceWeaponry(String userId, String selfWeaponryId){
        Map<String, WeaponryVO> weaponryVOMap = userServiceCache.getUserServiceMap().get(userId).getWeaponryVOMap();
        weaponryVOMap.remove(selfWeaponryId);
        asynDBManager.distributeWork(userId, ()->userWeaponryService.deleteBySelfWeaponryId(selfWeaponryId));
        MessageVO messageVO = new MessageVO(userId);
        routeUtils.sendWeaponryResponse(CommonBackpack.WEAPONRY_SUCCESS, userId,
                userId + "装备信息获取成功！", new ArrayList<>(userServiceCache
                        .getUserServiceMap().get(userId).getWeaponryVOMap().values()), messageVO);
        return true;
    }

    public boolean reduceWeaponry(String userId, List<WeaponryVO> newWeaponryVO){
        return false;
    }

}
